import maya.cmds as mc
import maya.mel as mel
class DynamicAddCurve():
    def DynamicAddCurve(self,dynCtrl='all_ctrl'):
        curveAll=mc.ls(sl=True)
        for i in curveAll:
            mc.select(i)
            dynamicCurve=str(i)+'_dynamic_curve'
            dynamicFollicle=str(i)+'_dynamic_follicle'

            
            mel.eval('makeCurvesDynamicHairs 1 0 1')
            ###  rename 
            follicle=mc.listRelatives(i,p=True)[0]
            mc.rename(follicle,dynamicFollicle)
            
            
            follicleGrpOrg=mc.listRelatives(dynamicFollicle,p=True)[0]
            follicleGrp=str(i)+'_'+str(follicleGrpOrg)
            mc.rename(follicleGrpOrg,follicleGrp)
            
            follicleShape=mc.listRelatives(dynamicFollicle,s=True)[0]
            dynamicCurveOrg=mc.listConnections(follicleShape+'.outCurve',d=True)[0]
            mc.rename(dynamicCurveOrg,dynamicCurve)
            
            outCurveGrpOrg=mc.listRelatives(dynamicCurve,p=True)[0]
            outCurveGrp=str(i)+'_'+str(outCurveGrpOrg)
            mc.rename(outCurveGrpOrg,outCurveGrp)
            
            ######
            hairSysOrg=mc.listConnections(str(follicleShape)+'.outHair',s=True)[0]
            hairSys=str(i)+'_hairSystem'
            mc.rename(hairSysOrg,hairSys)
            hairSysShape=mc.listRelatives(hairSys,s=True)[0]
            
            mc.setAttr(str(hairSysShape)+'.active',0)
            
            dynfolliShape=mc.listRelatives(dynamicFollicle,s=True)[0]
            mc.setAttr(str(dynfolliShape)+'.pointLock',1)
            
            if mc.attributeQuery('dynamic',node=dynCtrl,ex=True)==False:
                mc.addAttr(dynCtrl,longName='dynamic',at ="enum",en ="off:on:",k=True)
            else:
                pass
            if mc.attributeQuery('Swing_strength_Driven',node=dynCtrl,ex=True)==False:
                mc.addAttr(dynCtrl,longName='Swing_strength_Driven',at='float',min=0,max=1)
            else:
                pass
                
            if mc.attributeQuery('Swing_strength',node=dynCtrl,ex=True)==False:
                mc.addAttr(dynCtrl,longName='Swing_strength',at='float',min=0,max=1,dv=0.8)
            else:
                pass
            if mc.attributeQuery('dynamic_baseVaule',node=dynCtrl,ex=True)==False:
                mc.addAttr(dynCtrl,longName='dynamic_baseVaule',at='float',min=-1,max=1)
            else:
                pass
            if mc.attributeQuery('start_frame',node=dynCtrl,ex=True)==False:
                mc.addAttr(dynCtrl,longName='start_frame',at='float',dv=1)
            else:
                pass
            
            
            mc.setAttr(str(dynCtrl)+'.dynamic',k=True)
            mc.setAttr(str(dynCtrl)+'.Swing_strength_Driven',k=True)
            mc.setAttr(str(dynCtrl)+'.Swing_strength',k=True)
            mc.setAttr(str(dynCtrl)+'.dynamic_baseVaule',k=True)
            mc.setAttr(str(dynCtrl)+'.start_frame',k=True)
            mc.setDrivenKeyframe(str(hairSysShape)+'.simulationMethod', cd=str(dynCtrl)+'.dynamic',dv=0,v=1)
            mc.setDrivenKeyframe(str(hairSysShape)+'.simulationMethod', cd=str(dynCtrl)+'.dynamic',dv=1,v=3)
            ##  tomorrow  connect attribute
            mc.setAttr(str(follicleShape)+'.overrideDynamics',1)
            mc.setAttr(str(follicleShape)+'.stiffnessScale[1].stiffnessScale_FloatValue',1)
            mc.connectAttr(str(dynCtrl)+'.Swing_strength_Driven',str(follicleShape)+'.stiffness')
            mc.connectAttr(str(dynCtrl)+'.Swing_strength_Driven',str(follicleShape)+'.startCurveAttract')
            
            mc.setDrivenKeyframe(str(dynCtrl)+'.Swing_strength_Driven', cd=str(dynCtrl)+'.Swing_strength',dv=0,v=0.1,ott='linear',itt='linear')
            mc.setDrivenKeyframe(str(dynCtrl)+'.Swing_strength_Driven', cd=str(dynCtrl)+'.Swing_strength',dv=1,v=1,ott='linear',itt='linear')
            ####
            conNode=str(i)+'_condition'
            pma01=str(i)+'_01_PMA'
            pma02=str(i)+'_02_PMA'
            mc.createNode('condition',n=conNode)
            mc.createNode('plusMinusAverage',n=pma01)
            mc.createNode('plusMinusAverage',n=pma02)
            
            mc.connectAttr(str(dynCtrl)+'.Swing_strength_Driven',str(conNode)+'.firstTerm')
            mc.connectAttr(str(dynCtrl)+'.Swing_strength_Driven',str(conNode)+'.colorIfTrueR')
            mc.setAttr(str(conNode)+'.operation',2)
            mc.setAttr(str(conNode)+'.secondTerm',0.8)
            mc.setAttr(str(conNode)+'.colorIfFalseR',0.8)
            
            mc.connectAttr(str(conNode)+'.outColorR',str(pma01)+'.input2D[0].input2Dx')
            mc.setAttr(str(pma01)+'.input2D[1].input2Dx',0.8)
            mc.setAttr(str(pma01)+'.operation',2)
            
            mc.connectAttr(str(pma01)+'.output2Dx',str(pma02)+'.input2D[0].input2Dx')
            mc.connectAttr(str(dynCtrl)+'.dynamic_baseVaule',str(pma02)+'.input2D[1].input2Dx')
            mc.setAttr(str(pma02)+'.operation',1)
            
            mc.connectAttr(str(pma02)+'.output2Dx',str(follicleShape)+'.attractionScale[1].attractionScale_FloatValue')
            mc.setDrivenKeyframe(str(dynCtrl)+'.dynamic_baseVaule', cd=str(dynCtrl)+'.Swing_strength_Driven',dv=0.8,v=0.2,ott='linear',itt='linear')
            mc.setDrivenKeyframe(str(dynCtrl)+'.dynamic_baseVaule', cd=str(dynCtrl)+'.Swing_strength_Driven',dv=1,v=0.8,ott='linear',itt='linear')
            
            
            sourceAttr = mc.listConnections(str(hairSysShape)+'.startFrame',s=True, d=False ,p =True)
            pmaNode = str(hairSysShape)+'_startFrame'+'_pma'
            mc.createNode('plusMinusAverage' , n =  pmaNode )
            
            print(sourceAttr)
            mc.connectAttr( sourceAttr[0] , str(pmaNode)+'.input1D[0]' )
            mc.connectAttr( str(dynCtrl)+'.start_frame' , str(pmaNode)+'.input1D[1]' )
            
            mc.disconnectAttr(sourceAttr[0], str(hairSysShape)+'.startFrame' )
            mc.connectAttr(  str(pmaNode)+'.output1D' ,str(hairSysShape)+'.startFrame'  )
            #
            #mc.connectAttr(str(dynCtrl)+'.start_frame',str(hairSysShape)+'.startFrame')
            #
            mc.setAttr(str(dynCtrl)+'.dynamic_baseVaule',k=False)
            mc.setAttr(str(dynCtrl)+'.Swing_strength_Driven',k=False)
            
            
            grpAll=str(i)+'_dynamic_grp'
            mc.group(follicleGrp,outCurveGrp,hairSys,n=grpAll)